

function PageContextBsgrid(settings){
	PageContext.call(this, settings);
	var _this = this; //把this保存下来，以后用_this代替this，这样就不会被this弄晕了
	//表格-内容
	 var url = $("#"+_this.setting.formId).attr("action");
	 if(!url){
		return false; 
	 }
	 if(url.indexOf("?")>-1){
		 url+="&noPage="+!_this.setting.canPage;
	 }else{
		 url+="?noPage="+!_this.setting.canPage;
	 }
	var defaults = {
	   	    url:url
	       ,autoLoad: false
	       ,first:true
	       ,fwAutoLoad: true
	       ,pageSizeSelect: true
	       ,showPageToolbar:_this.setting.canPage
	       ,stripeRows: true
	       ,pageSize: _this.setting.canPage?10:null
	       ,pageIncorrectTurnAlert:false
	       ,displayBlankRows:false
	       ,extend: {
	           settings: {
	               supportColumnMove: true // if support extend column move, default false
	           } 
	       } 
 		   ,additionalAfterRenderGrid: function (parseSuccess, gridData, options) {
 			  _this.refushOperTdClass();
 			  _this.renderAfterEvent();
 			  _this.renderAfterEventOther();
 			  _this.afterRenderGrid(gridData);
 			 pageInit();
	       }
 		   ,additionalRenderPerRow: function (record, rowIndex, trObj, options) {
 			  _this.additionalRenderPerRow(record, rowIndex, trObj);
	       }
 		   ,additionalRenderPerColumn:function (record, rowIndex, colIndex, tdObj, trObj, options) {
 			  _this.additionalRenderPerColumn(record, rowIndex, colIndex, tdObj, trObj, options);
 		   }
 		   ,requestParamsName: {
                curPage: 'page'
           }
	    };
	this.option = $.extend( true,  {}, defaults, settings.option || {}); 
	
	/**
	 * 当前页面上下文
	 */
	this.formInit=function(layero){
		 
	}//更新后操作
	this.updateItemAfter=function(result,para){
		_this.gridObj.refreshPage();
	 };
	 this.addItemAfter=function(result,para){
		 _this.gridObj.refreshPage();
	 }; 
	 this.deleteItemAfter=function(id){
		 _this.gridObj.refreshPage();
	 };
	 
	 this.removeItemAfter=function(ids){
		 if(!isNaN(ids)){
		    var trObj = $("#"+_this.setting.tableId+"_"+ids);
		    $("#"+_this.setting.tableId+"_"+ids+"_"+"validModDt").text(_this.getNowFormatDate());
		    _this.setDeleteClass($("#"+_this.setting.tableId+"_tr_"+ids),true);
		 }else{
			var array = ids.split(",");
			for (x in array)
			{
				var id = array[x];
				_this.setDeleteClass($("#"+_this.setting.tableId+"_tr_"+id),true);
				var trObj = $("#"+_this.setting.tableId+"_"+id);
				$("#"+_this.setting.tableId+"_"+id+"_"+"validModDt").text(_this.getNowFormatDate());
			}
		}
	 };
	 this.init=function(option){//初始化
		 _this.option = $.extend( true,  {}, _this.option, option|| {}); 
		 _this.gridObj = $.fn.bsgrid.init(_this.setting.tableId,_this.option );
	 		$("#"+_this.setting.formId).validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
	 		//搜索
	 		if(_this.option.fwAutoLoad){
	 			_this.search();   
	 		}
			//页面初始化
	 		_this.initEvent();
	 }
	 this.additionalRenderPerColumn = function(record, rowIndex, colIndex, tdObj, trObj, options){//搜索
		   var name = '';
		   var render = options.columnsModel[colIndex].render;
		   if(render!=''){
			  name = render;
		   }else{
			  var index = options.columnsModel[colIndex].index;
			  if(index!=''){
				 name = index;
			   }
		   }
		   if(name!='' && name!=undefined){
			   tdObj.attr("id",trObj.attr("id")+"_"+name);
		   }
	 }
	 this.additionalRenderPerRow = function(record, rowIndex, trObj){//搜索
		  var trKeyValue = record.id;
          trObj.attr("id",_this.setting.tableId+"_tr_"+trKeyValue);
          trObj.attr("val",trKeyValue);
          trObj.attr("label",record.name);
          trObj.attr("rowIndex",rowIndex);
	 }
	 this.search = function(){//搜索
		 var from = $("#"+_this.setting.formId);
		 if(from.validationEngine("validate")){
			 if(_this.searchCheck()){
				 var searchParames = '' ; 
				 searchParames = from.formSerializeNotBlank();   
				 _this.gridObj.search(searchParames);
			 }
		 }
	 } ;
	 this.getCheckedIds = function() {//获取表格选中的id
		 var records = _this.getCheckedRecords();
		 var ids = new Array();
		 if(records){
			for(var i = 0; i < records.length; i++) {
				ids.push(_this.gridObj.getRecordIndexValue(records[i], 'id'));
			}
		 }
		 return ids.join(",");
	 };
	 this.getCheckedRecords=function() {//获取表格选中的项
		 var records = new Array();
		 $('#'+_this.setting.tableId+' tbody tr').each(function() {
		 	if($(this).find('td:eq(0)>input:checked').length == 1){
		     		records[records.length] = _this.gridObj.getRowRecord($(this));
		     	}
		     });
		     return records;
		 }
	 this.refushOperTdClass = function(){
		 var tds = $($("#"+_this.setting.tableId+" tbody tr .operBtn")).parent('div').parent('div').parent('td');
		 tds.removeClass("lineNoWrap").addClass("lineNoWrapOper");
	 }
};
//表格的checkbox
function checkboxFunc(record, rowIndex, colIndex, options) {
	return '<input type="checkbox" value="' + pageContext.gridObj.getColumnValue(rowIndex, pageContext.gridObj.getColumnModel(colIndex).index) + '"/>';
}
//表格的操作栏
function operateFunc(record, rowIndex, colIndex, options) {
	var id = pageContext.gridObj.getColumnValue(rowIndex, pageContext.gridObj.getColumnModel(colIndex).index);
	var html= pageContext.getPingPuOperBtn(id,record.name,record) + pageContext.getOperBtn(id,record.name,record);
	return html;
} 

function removeClassFunc(record, rowIndex, colIndex, options,index) {
	var value = pageContext.gridObj.getRecordIndexValue(record, 'deleteClass');
	var id = pageContext.gridObj.getRecordIndexValue(record, 'id');
	var v = ""
	var label = "未删除";
	var checkedAttr = "";
	var classAttr = "";
	if(value=="true"||value==true){
		label = "已删除";
		v = "false";
		checkedAttr = "";
		classAttr = "warn-label label-danger ";
	}else{
		classAttr = "label-success";
		v = "checked";
		checkedAttr = "checked='"+v+"'";
	}
	if(pageContext.setting.canTdRemove && (record.canUpdate==undefined ||record.canUpdate)){
		return '<label><input alias="deleteClass" name="deleteClass" value="'+value+'" val="'+id+'"  class="ace ace-switch ace-switch-4 btn-empty" type="checkbox" '+checkedAttr+' /><span class="lbl"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span></label>';
	}else{
		return '<label><span class="lbl1"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span></label>';
	}
}
function validClassFunc(record, rowIndex, colIndex, options,index) {
	var value = pageContext.gridObj.getRecordIndexValue(record, 'validClass');
	var id = pageContext.gridObj.getRecordIndexValue(record, 'id');
	var v = true;
	var label = "有效";
	var checkedAttr = "checked";
	var classAttr = "label-success";
	if(value=="false"||value==false){
		label = "无效";
		checkedAttr = "";
		classAttr = "label-danger warn-label";
	}else{
		checkedAttr = "checked='"+v+"'";
	}
	if(pageContext.setting.canTdValid && (record.canUpdate==undefined ||record.canUpdate)){
		return '<label><input alias="validClass" name="validClass" value="'+value+'" val="'+id+'"  class="ace ace-switch ace-switch-4 btn-empty" type="checkbox" '+checkedAttr+' /><span class="lbl"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span></label>';
	}else{
		return '<label><span class="lbl1"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span>';
	}
}  

function booleanClassFunc(record, rowIndex, colIndex, options,index,labelName) {
	var value = pageContext.gridObj.getRecordIndexValue(record, index);
	var id = pageContext.gridObj.getRecordIndexValue(record, 'id');
	var v = true;
	var label = "是";
	var checkedAttr = "checked";
	var classAttr = "label-success";
	if(value=="false"||value==false){
		label = "否";
		checkedAttr = "";
		classAttr = "label-danger warn-label";
	}else{
		checkedAttr = "checked='"+v+"'";
	}
	if(!labelName){
		labelName = "";
	}
	if(pageContext.setting.canUpdate && (record.canUpdate==undefined ||record.canUpdate)){
		return '<label><input alias="booleanClass" label="'+labelName+'"  name="'+index+'" value="'+value+'" val="'+id+'"  class="ace ace-switch ace-switch-4 btn-empty" type="checkbox" '+checkedAttr+' /><span class="lbl"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span></label>';
	}else{
		return '<label><span class="lbl1"><span style="margin-left:5px;margin-right:5px;margin-top:1px" class="label '+classAttr+'">'+label+'</span></span>';
	}
}  
